﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class systemconfig_KechengPeizhi : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //string sql = string.Format("select count(id) from JiGouPeiZhi where JiGouMingCheng='{0}'", Session["sJigou"].ToString());
            //int num = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, sql, null));
            //if (num == 0)
            //{
            //    sql = string.Format("insert into JiGouPeiZhi (JiGouMingCheng) values('{0}')", Session["sJigou"].ToString());
            //    num = DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
            //    if (num == 1)
            //    {
            //        CheckBox1.Checked = false;
            //    }
            //    else
            //    {
            //        Response.Write(string.Format("<script language='javascript'>alert('{0}');</script>", "机构映射失败"));
            //    }
            //}
            //else
            //{
            //    sql = string.Format("select kecheng,laoshi from JiGouPeiZhi where JiGouMingCheng='{0}'", Session["sJigou"].ToString());
            //    string tag = string.Empty;
            //    object obj = DbHelper.ExecuteScalar(CommandType.Text, sql, null);
            //    if (obj != null)
            //    {
            //        tag = obj.ToString();
            //    }
            //    try
            //    {
            //        CheckBox1.Checked = true;
            //    }
            //    catch
            //    {
            //        CheckBox1.Checked = false;
            //    }
            //}

            Check_Brid();
        }
    }

    private void Check_Brid()
    {
        string sqlText = string.Format("select kecheng from  JiGouPeiZhi where JiGouMingCheng = '{0}'", Session["sJigou"].ToString());
        string id = DbHelper.ExecuteScalar(CommandType.Text, sqlText, null).ToString();
        if (id == "1")
        {
            this.CheckBox1.Checked = true;
        }
        else
        {
            this.CheckBox1.Checked = false;
        }

    }


    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        //string sql = string.Format("select count(id) from JiGouPeiZhi where JiGouMingCheng='{0}'", Session["sJigou"].ToString());
        //int num = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, sql, null));
        //if (num == 0)
        //{
        //    sql = string.Format("insert into JiGouPeiZhi (JiGouMingCheng) values('{0}')", Session["sJigou"].ToString());
        //    num = DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
        //    if (num == 1)
        //    {
        //        sql = string.Format("update JiGouPeiZhi set kecheng='{0}',laoshi='{0}' where JiGouMingCheng='{1}'", CheckBox1.Checked, Session["sJigou"].ToString());
        //        DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
        //    }
        //    else
        //    {
        //        Response.Write(string.Format("<script language='javascript'>alert('{0}');</script>", "机构映射失败"));
        //    }
        //}
        //else
        //{
        //    sql = string.Format("update JiGouPeiZhi set kecheng='{0}',laoshi='{0}' where JiGouMingCheng='{1}'",CheckBox1.Checked, Session["sJigou"].ToString());
        //     DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
        //}
        string sqlText1 = string.Format("select count(id) from JiGouPeiZhi where JiGouMingCheng = '{0}'", Session["sJigou"].ToString());
        int id = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, sqlText1, null).ToString());
        string j = CheckBox1.Checked ? "1" : "0";
        int i = 0;
        if (id > 0)
        {           
            string sqlText = string.Format("update JiGouPeiZhi set kecheng = '{0}'", j);
            i = Convert.ToInt32(DbHelper.ExecuteNonQuery(CommandType.Text, sqlText, null).ToString());          
        }
        else
        {
            string sqlText = string.Format("insert into JiGouPeiZhi(JiGouMingCheng,kecheng) values('{0}','{1}')", Session["sJigou"].ToString(), j);
            i = Convert.ToInt32(DbHelper.ExecuteNonQuery(CommandType.Text, sqlText, null).ToString());          
        }
      if (i > 0)
        {
            Response.Write("<script>alert('配置成功');location.href='KechengPeizhi.aspx'</script>");
        }
        else
        {
            Response.Write("<script>alert('配置失败');location.href='KechengPeizhi.aspx'</script>");
        }

       
        

    }
}
